package com.zp.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.bo.OperationDictBo;
import com.zp.base.entity.vo.OperationDictVo;
import com.zp.base.service.IOperationDictService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 手术操作字典控制器
 * 前端访问路由地址为:/base/operationDict
 *
 * @author zhang peng
 * @date 2023-01-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operationDict")
public class OperationDictController extends BaseController {

    private final IOperationDictService iOperationDictService;

    /**
     * 查询手术操作字典列表 (分页)
     */
    @SaCheckPermission("base:operationDict:list")
    @GetMapping("/selectPage")
    public TableDataInfo<OperationDictVo> selectPage (OperationDictBo bo, PageQuery pageQuery) {
        return iOperationDictService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询手术操作字典列表 (不分页)
     */
    @SaCheckPermission("base:operationDict:list")
    @GetMapping("/list")
    public R<List<OperationDictVo>> list (OperationDictBo bo) {
        List<OperationDictVo> list = iOperationDictService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出手术操作字典列表
     */
    @SaCheckPermission("base:operationDict:export")
    @Log(title = "手术操作字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (OperationDictBo bo, HttpServletResponse response) {
        List<OperationDictVo> list = iOperationDictService.queryList(bo);
        ExcelUtil.exportExcel(list, "手术操作字典", OperationDictVo.class, response);
    }

    /**
     * 获取手术操作字典详细信息
     *
     * @param operationDictId 主键
     */
    @SaCheckPermission("base:operationDict:query")
    @GetMapping("/{operationDictId}")
    public R<OperationDictVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String operationDictId) {
        return R.ok(iOperationDictService.queryById(operationDictId));
    }

    /**
     * 新增手术操作字典
     */
    @SaCheckPermission("base:operationDict:add")
    @Log(title = "手术操作字典", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody OperationDictBo bo) {
        return toAjax(iOperationDictService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改手术操作字典
     */
    @SaCheckPermission("base:operationDict:edit")
    @Log(title = "手术操作字典", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody OperationDictBo bo) {
        return toAjax(iOperationDictService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除手术操作字典
     *
     * @param operationDictIds 主键串
     */
    @SaCheckPermission("base:operationDict:remove")
    @Log(title = "手术操作字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operationDictIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] operationDictIds) {
        return toAjax(iOperationDictService.deleteWithValidByIds(Arrays.asList(operationDictIds), true) ? 1 : 0);
    }

    /**
     * 手术项目自动补全
     *
     * @return
     */
    @PostMapping("/getOperationAuto")
    public R<List<OperationDictVo>> getOperationAuto (@RequestBody OperationDictBo bo) {
        return R.ok(iOperationDictService.getOperationAuto(bo));
    }
}
